package com.zhangxu.microservice.auth.dao.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangxu.microservice.auth.dao.condition.SystemCondition;
import com.zhangxu.microservice.auth.dao.entity.SystemDO;
import com.zhangxu.microservice.auth.dao.mapper.SystemMapper;
import com.zhangxu.microservice.auth.dao.service.SystemDao;
import org.springframework.stereotype.Repository;

import java.io.Serializable;

/**
 * 系统管理DAO实现类
 */
@Repository
public class SystemDaoImpl extends ServiceImpl<SystemMapper, SystemDO> implements SystemDao {

    @Override
    public SystemDO getBySystemCode(String systemCode) {
        return this.lambdaQuery()
                .eq(SystemDO::getSystemCode, systemCode)
                .eq(SystemDO::getDeleteTime, 0L)
                .one();
    }

    @Override
    public SystemDO getById(Serializable id) {
        return this.lambdaQuery()
                .eq(SystemDO::getId, id)
                .eq(SystemDO::getDeleteTime, 0L)
                .one();
    }

    @Override
    public IPage<SystemDO> page(SystemCondition condition) {
        condition.checkPage();
        return this.lambdaQuery()
                .like(StrUtil.isNotBlank(condition.getName()), SystemDO::getName, condition.getName())
                .eq(StrUtil.isNotBlank(condition.getSystemCode()), SystemDO::getSystemCode, condition.getSystemCode())
                .eq(condition.getStatus() != null, SystemDO::getStatus, condition.getStatus())
                .eq(condition.getOwnerId() != null, SystemDO::getOwnerId, condition.getOwnerId())
                .eq(SystemDO::getDeleteTime, 0L)
                .orderByAsc(SystemDO::getSort)
                .orderByDesc(SystemDO::getCreateTime)
                .page(condition.getIpage());
    }
}